<?php defined('BASEPATH') or exit('No direct script access allowed');
/**
 *
 * @author  PyroCMS Dev Team
 * @package PyroCMS\Core\Modules\Blog\Controllers
 */
class Admin extends Admin_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->db->dbprefix="l_";
		$this->load->model('channel_m');
		$this->load->model('channel_result_m');
		$this->load->helper('url');
		$this->lang->load('channel');
		$this->template
		->append_css('module::channel.css')
		->append_css('libs/960_24_col.css')
		->append_css('libs/daterangepicker.css')
		->append_js('libs/date.js')
		->append_js('libs/daterangepicker.js')
		->append_js('module::channel.js');
	}

	/**
	 * Show all created blog posts
	 */
	public function index()
	{
		$this->load->helper('form');

		// Params
		$channel_id =$this->input->post('channel_id');
		$fromdate =$this->input->post('fromdate');
		$todate =$this->input->post('todate');

		$channel_id = $channel_id?$channel_id:1;
		$fromdate = $fromdate?$fromdate:date('Y-m-d');
		$todate = $todate?$todate:date('Y-m-d');

		$metaModel=array(
			"channel_id"=>$channel_id,
			"fromdate"=>$fromdate,
			"todate"=>$todate
		);

		$channelModel =new Channel_m();
		$channelList = $channelModel->get_all();

		$channelResultModel =new Channel_Result_m();
		$result = $channelResultModel->getByDateRange($channel_id, $fromdate, $todate);

		$this->template
		->set('channelList',$channelList)
		->set('metaModel',$metaModel)
		->set('dateList',$result['dateList'])
		->set('detailList',$result['detailList'])
		->build('admin/index');
	}

	/**
	 * Show all created blog posts
	 */
	public function edit()
	{
		$this->load->helper('form');

		// Params
		$channel_id =$this->input->get('channel_id');
		$open_date =$this->input->get('open_date');
		$open_date = $open_date?$open_date:date('Y-m-d');
		$channel_id = $channel_id?$channel_id:1;

		$channelModel =new Channel_m();
		$channelList = $channelModel->get_all();

		$channelResultModel =new Channel_Result_m();
		$detail = $channelResultModel->tryGetByDate($channel_id, $open_date);


		$metaModel=array(
			"channel_id"=>$channel_id,
			"open_date"=>$open_date
		);

		$this->template
		->set('channelList',$channelList)
		->set('metaModel',$metaModel)
		->set('detail',$detail)
		->build('admin/edit');
	}


	/**
	 * Show all created blog posts
	 */
	public function editPost()
	{
		$this->load->helper('form');

		// Params
		$channel_id =$this->input->post('channel_id');
		$open_date =$this->input->post('open_date');
		$open_date = $open_date?$open_date:date('Y-m-d');
		$channel_id = $channel_id?$channel_id:1;

		$prizeList=array();
		for($i=0;$i<50;$i++){
			$prize=$this->input->post('prize'.$i);
			if(!$prize){
				break;
			}
			$prizeList[]=$prize;
		}

		$channelResultModel =new Channel_Result_m();
		$detail = $channelResultModel->insertOrUpdateResult($channel_id, $open_date, $prizeList);

		redirect('/admin/channel/edit?channel='.$channel_id.'&open_date='.$open_date);
	}

	public function crawl()
	{
		$today =new DateTime();
		$url=base_url().'cron/channel_result/crawl.php?open_date='.$today->format('Y-m-d');
		redirect($url);
	}

}
